package models.common.user;

/** 
 * Property & Copyright 2013 Nokia Siemens Networks.
 */

import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import play.data.validation.Email;
import play.data.validation.Required;
import play.db.jpa.Model;

@Entity
@Table(name="das_user")
public class DASUser extends Model{
	
	@Required
	public String name;
	
	@Required
	public String password;

	@Required
	@Email
	public String email;

	public String nsnId;
	
	@ManyToMany(fetch=FetchType.LAZY)
    public Set<DASUserGroup> userGroup;
	
	public static DASUser connect(String nsnId, String password){
		return DASUser.find("byNsnIdAndPassword", nsnId, password).first();
	}
	
	public Set<DASUserGroup> getDASUserGroup(){
		return this.userGroup;
	}
	
	public Set<DASUserRole> getDASUserRole(){
		
		Set<DASUserGroup> userGroup = getDASUserGroup();
		Set<DASUserRole> userRole = new HashSet<DASUserRole>();
		
		for(DASUserGroup group : userGroup){
			userRole.addAll(group.getDASUserRole());
		}
		
		return userRole;
	}

	public String getUserName() {
		// TODO Auto-generated method stub
		return name;
	}

	public String getPassword() {
		// TODO Auto-generated method stub
		return password;
	}
}
